#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <algorithm>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    score = -1.0;
    ui->setupUi(this);
    ui->stackedWidget->setVisible(false);

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
//    int a = 6;
//    int b = 1;
//    //r表示register，加=表示是输出
//    asm volatile(
//                "movl %0, %1\n"
//                "add %0, %1\n"
//                "add %0, %0\n"


///* %0 */        : "=r" (a)
///* %1 */        : "r" (b)
//    );


    int a = 40;
    int b;
    asm (
        "mov %1, %%eax; \
        add %1, %0; "
//        mov %%eax, %0;"
        : "=a" (b)
        : "a" (a)


    );

    QString str;
    str += "a = ";
    str += QString::number(a, 10);
    str += "\nb = ";
    str += QString::number(b, 10);
    ui->label->setText(str);
}


void MainWindow::on_lineEdit_editingFinished()
{
    QString score = ui->lineEdit->text();
    double src = score.toDouble();
    double grade = -1;
    double inc = 32.5;
#if defined(__x86_64__)
    asm volatile(
//                "movq %1, %0; \
//                 addq %2, %0"
//                "mov $1, eax "

                "movq %1, %0; \
                addq $2, %0"

                : "=r" (grade)
                : "r" (src), "r" (inc)
                );
#endif
    if(grade != -1) this->score = grade;
    QString res;
    if(this->score >= 90) res = 'A';
    else if(this->score < 90 && this->score >= 75) res = 'B';
    else if(this->score < 75 && this->score >= 60) res = 'C';
    else if(this->score < 60 && this->score >= 0) res = 'D';
    //ui->label->setText(QString::number(this->score, 'f', 10));
    ui->label->setText(res);
}

QString MainWindow::getClassification(){
    QString result;


#if defined(__x86_64__)




#endif


    return result;
}

bool MainWindow::checkDot()
{
    QString scoreString = QString::number(score, 'f', 10);
    int a;
//#if defined(__x86_64__)
//    asm volatile(

//                "movl %1, %%dx;   \
//                movl $2, %%cl;   \
//                cmp $1, %%cl;    \
//                sub $1, %%cl;   \
//                inc %%si"



//                : "=r" (a)
//                : "r" (score)
//                );
//#endif
    return false;
}


void MainWindow::on_actionScore_triggered()
{
    ui->stackedWidget->setVisible(true);
    ui->stackedWidget->setCurrentWidget(ui->page_score);
}


void MainWindow::on_actionSecond_Large_triggered()
{
    ui->stackedWidget->setVisible(true);
    ui->stackedWidget->setCurrentWidget(ui->page_secondlarge);
}


void MainWindow::on_button_over_clicked()
{
    QList<int> nums;
//    int nums[10];
    nums.append(ui->sl1->text().toInt());
    nums.append(ui->sl2->text().toInt());
    nums.append(ui->sl3->text().toInt());
    nums.append(ui->sl4->text().toInt());
    nums.append(ui->sl5->text().toInt());
    nums.append(ui->sl6->text().toInt());
    nums.append(ui->sl7->text().toInt());
    nums.append(ui->sl8->text().toInt());
    nums.append(ui->sl9->text().toInt());
    nums.append(ui->sl10->text().toInt());
//    nums[1] = ui->sl2->text().toInt();
//    nums[2] = ui->sl3->text().toInt();
//    nums[3] = ui->sl4->text().toInt();
//    nums[4] = ui->sl5->text().toInt();
//    nums[5] = ui->sl6->text().toInt();
//    nums[6] = ui->sl7->text().toInt();
//    nums[7] = ui->sl8->text().toInt();
//    nums[8] = ui->sl9->text().toInt();
//    nums[9] = ui->sl10->text().toInt();

    qSort(nums.begin(), nums.end(), compareNum);

//    int max = nums[0];
//    for(int i = 0; i <= 10; i++){
//        if(nums[i] > max) max = nums[i];
//    }

//    int sl = max, count = 0;
//    for(int i = 0; i <= 10; i++){
//        if(nums[i] == sl) continue;
//        else if(nums[i] < sl){
//            ++count;
//            if(count <= 1) sl = nums[i];
//        }
//    }

    QString res;
    for(int i = 0; i < 10; i++){
        res += QString::number(nums[i], 10);
        res += " ";
    }
    ui->sl_res->setText(res);
}

bool MainWindow::compareNum(int num1, int num2){
    return num1 > num2 ? true : false;
}

